package com.demo.demo.images;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
/**
 * Copyright 缩放第一张图片
 *
 * @Author WuHuang
 * @Date 2023/5/11
 */
public class ImageMergerExample {

    public static void main(String[] args) throws IOException {
        // 读取原始图片
        File file = new File("D:\\WorkSpace\\wu\\spring-boot-study\\springboot-demo\\src\\main\\resources\\leee.png");
        BufferedImage image = ImageIO.read(file);

//        // 缩放图片
////            int desiredWidth = 220; // 指定宽度
//        int desiredWidth = 600; // 指定宽度
////            int desiredHeight = 135; // 指定高度
//        int desiredHeight = 381; // 指定高度
//        BufferedImage resizedImage = new BufferedImage(desiredWidth, desiredHeight, image.getType());
//        Graphics2D g = resizedImage.createGraphics();
//        g.drawImage(image, 0, 0, desiredWidth, desiredHeight, null);
//        g.dispose();
        Graphics2D g = image.createGraphics();
        FontMetrics fm = g.getFontMetrics();
        int labelHeight = fm.getHeight();
        // 添加价格标签
        Font font = new Font("苹方-简", Font.BOLD, 62);
        Color textColor1 = Color.WHITE;
        Color redBackgroundColor = new Color(201, 17, 47);
        int x1 = 44;
        int y1 = 65;
        addPriceTag(image.createGraphics(), "活动价: ¥1380.00", x1, y1, font, textColor1, redBackgroundColor, false);

        Font font2 = new Font("苹方-简", Font.BOLD, 58);
        Color textColor2 = Color.BLACK;
        Color stripedBackgroundColor = Color.WHITE;
        int x2 = 77;
        int y2 = 250;
        addPriceTag(image.createGraphics(), "吊牌价: ¥1080.00", x2, y2, font2, textColor2, stripedBackgroundColor, true);

        // 保存图像
        File outputImageFile = new File("D:\\WorkSpace\\wu\\spring-boot-study\\springboot-demo\\src\\main\\resources\\output2.png");
        ImageIO.write(image, "png", outputImageFile);
    }

    private static void addPriceTag(Graphics2D g2d, String price, int x, int y,
                                    Font font, Color textColor, Color backgroundColor, boolean drawLine) {
        // 获取字体度量值
        FontMetrics fm = g2d.getFontMetrics(font);
        int textWidth = fm.stringWidth(price);
        int labelHeight = fm.getHeight();
        // 计算背景大小和位置

        //标签的宽度
        int width1 = textWidth + 40;
        //标签的高度
        int height1 = 60;

        // 绘制背景
//        g2d.setColor(backgroundColor);
//        if (drawLine) {
//            g2d.fillRect(x - 18, y - 35, width1, height1 - 10);
//        }else {
//            g2d.fillRect(x - 18, y  - 45, width1, height1 + 15);
//        }

        // 绘制文字
        g2d.setFont(font);
        g2d.setColor(textColor);
        g2d.drawString(price, x, y + labelHeight);
        // 绘制删除线
        if (drawLine) {

            g2d.setRenderingHint(RenderingHints.KEY_STROKE_CONTROL, RenderingHints.VALUE_STROKE_PURE);
            g2d.drawLine(x + 200, y + 58 , x + textWidth + 5, y + 58);

        }
    }
}

